package org.lanyu.springainovel.chat.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.lanyu.springainovel.common.entity.SpringAIChatMemory;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 聊天记忆 Mapper 接口（合并了ChatMemoryMapper和SpringAIChatMemoryMapper�? * 对应 spring_ai_chat_memory �? */
@Mapper
public interface ChatMemoryMapper extends BaseMapper<SpringAIChatMemory> {
    
    /**
     * 根据会话ID获取最近的N条记�?     * 
     * @param conversationId 会话ID
     * @param limit 限制条数
     * @return 聊天记录列表
     */
    @Select("SELECT * FROM spring_ai_chat_memory WHERE conversation_id = #{conversationId} " +
            "ORDER BY content_Index ASC, " +
            "CASE WHEN type = 'USER' THEN 0 ELSE 1 END LIMIT #{limit}")
    List<SpringAIChatMemory> getRecentMessages(@Param("conversationId") String conversationId, @Param("limit") int limit);
    
    /**
     * 根据会话ID获取所有记�?     * 
     * @param conversationId 会话ID
     * @return 聊天记录列表
     */
    @Select("SELECT * FROM spring_ai_chat_memory WHERE conversation_id = #{conversationId} " +
            "ORDER BY content_Index ASC, " +
            "CASE WHEN type = 'USER' THEN 0 ELSE 1 END")
    List<SpringAIChatMemory> getAllMessages(@Param("conversationId") String conversationId);
    
    /**
     * 获取所有会话ID
     * 
     * @return 会话ID列表
     */
    @Select("SELECT DISTINCT conversation_id FROM spring_ai_chat_memory")
    List<String> getAllConversationIds();
    
    /**
     * 获取指定会话的最大content_index
     * 
     * @param conversationId 会话ID
     * @return 最大content_index
     */
    @Select("SELECT MAX(content_Index) FROM spring_ai_chat_memory WHERE conversation_id = #{conversationId}")
    Integer getMaxContentIndex(@Param("conversationId") String conversationId);
    
    /**
     * 根据会话ID删除所有记�?     * 
     * @param conversationId 会话ID
     * @return 删除的记录数
     */
    @Delete("DELETE FROM spring_ai_chat_memory WHERE conversation_id = #{conversationId}")
    int deleteByConversationId(@Param("conversationId") String conversationId);
    
    /**
     * 删除指定contentIndex及之后的所有记�?     * 
     * @param conversationId 会话ID
     * @param contentIndex 起始content_index
     * @return 删除的记录数
     */
    @Delete("DELETE FROM spring_ai_chat_memory WHERE conversation_id = #{conversationId} AND content_Index >= #{fromContentIndex}")
    int deleteFromContentIndex(@Param("conversationId") String conversationId, @Param("fromContentIndex") int fromContentIndex);
    
    /**
     * 删除指定时间戳及之后的所有消息
     * @param conversationId 会话ID
     * @param fromTimestamp 时间戳
     * @return 删除的记录数
     */
    @Delete("DELETE FROM spring_ai_chat_memory WHERE conversation_id = #{conversationId} AND timestamp >= #{fromTimestamp}")
    int deleteFromTimestamp(@Param("conversationId") String conversationId, @Param("fromTimestamp") LocalDateTime fromTimestamp);
}
